import time
import _thread
from rpi_ws281x import PixelStrip, Color
import fire
import threading

LED_COUNT = 256       # LED灯的个数
LED_PIN = 18       # DI端接GPIO18
LED_BRIGHTNESS = 3  # Set to 0 for darkest and 255 for brightest

# 以下可以不用改
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10          # DMA channel to use for generating signal (try 10)
# True to invert the signal (when using NPN transistor level shift)
LED_INVERT = False
LED_CHANNEL = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53

GifInfo = [
    [
        [1, 1, 1],
        [1, 0, 1],
        [1, 0, 1],
        [1, 0, 1],
        [1, 1, 1]],
    [
        [0, 0, 1],
        [0, 0, 1],
        [0, 0, 1],
        [0, 0, 1],
        [0, 0, 1]],
    [
        [1, 1, 1],
        [0, 0, 1],
        [1, 1, 1],
        [1, 0, 0],
        [1, 1, 1]],
    [
        [1, 1, 1],
        [0, 0, 1],
        [1, 1, 1],
        [0, 0, 1],
        [1, 1, 1]],
    [
        [1, 0, 1],
        [1, 0, 1],
        [1, 1, 1],
        [0, 0, 1],
        [0, 0, 1]],
    [
        [1, 1, 1],
        [1, 0, 0],
        [1, 1, 1],
        [0, 0, 1],
        [1, 1, 1]],
    [
        [1, 1, 1],
        [1, 0, 0],
        [1, 1, 1],
        [1, 0, 1],
        [1, 1, 1]],
    [
        [1, 1, 1],
        [0, 0, 1],
        [0, 0, 1],
        [0, 0, 1],
        [0, 0, 1]],
    [
        [1, 1, 1],
        [1, 0, 1],
        [1, 1, 1],
        [1, 0, 1],
        [1, 1, 1]],
    [
        [1, 1, 1],
        [1, 0, 1],
        [1, 1, 1],
        [0, 0, 1],
        [1, 1, 1]]]
GifRgb = [
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [0, 0, 0]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [0, 0, 0]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [0, 0, 0]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]],
    [
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[255, 255, 255], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]],
        [[0, 0, 0], [0, 0, 0], [255, 255, 255]],
        [[255, 255, 255], [255, 255, 255], [255, 255, 255]]]]

Font5X3 = [
    [0, 0, 0],
    [0, 1, 0],
    [0, 0, 0],
    [0, 1, 0],
    [0, 0, 0]
]
Rgb5X3 = [
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
    [[0, 0, 0], [255, 255, 255], [0, 0, 0]],
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
    [[0, 0, 0], [255, 255, 255], [0, 0, 0]],
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
]

Font8X8 = [
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1]
]
Rgb8X8 = [
    [[0, 145, 255], [0, 145, 255], [255, 255, 0], [0, 145, 255], [
        0, 145, 255], [255, 255, 255], [255, 255, 255], [0, 145, 255]],
    [[0, 145, 255], [0, 145, 255], [0, 145, 255], [0, 145, 255],
     [150, 0, 0], [150, 0, 0], [0, 145, 255], [0, 145, 255]],
    [[0, 255, 255], [0, 255, 255], [0, 145, 255], [150, 0, 0],
     [150, 0, 0], [109, 1, 1], [150, 0, 0], [150, 0, 0]],
    [[0, 255, 255], [150, 0, 0], [150, 0, 0], [150, 0, 0], [
        109, 1, 1], [255, 255, 255], [109, 1, 1], [150, 0, 0]],
    [[109, 1, 1], [109, 1, 1], [109, 1, 1], [109, 1, 1], [
        255, 255, 255], [255, 255, 255], [255, 255, 255], [109, 1, 1]],
    [[0, 255, 255], [255, 255, 255], [109, 1, 1], [255, 255, 255], [
        255, 255, 255], [140, 0, 0], [255, 255, 255], [0, 255, 255]],
    [[0, 145, 255], [255, 255, 255], [109, 1, 1], [255, 255, 255], [
        255, 255, 255], [255, 255, 255], [255, 255, 255], [0, 145, 255]],
    [[0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0], [
        0, 255, 0], [0, 255, 0], [0, 255, 0], [0, 255, 0]]
]

Font1X3 = [
	[1,1,1]
]
Rgb1X3 = [
	[[0, 0, 255],[0, 0, 255],[0, 0, 255]]
]
def light_pos(strip, x, y, r, g, b):
    if x % 2 == 0:
        pos = x * 8 + y
    else:
        pos = x * 8 + 7 - y
    strip.setPixelColor(pos, Color(r, g, b))


def colorWipe(strip):
    """一次擦除显示像素的颜色."""
    for i in range(strip.numPixels()):
        strip.setPixelColor(i, Color(0, 0, 0))
    strip.show()


def ReadALL(strip, index, pos):
    array = GifInfo
    rgb = GifRgb
    height = len(array[0])
    width = len(array[0][0])
    for j in range(height):
        for k in range(width):
            if array[index][j][k] == 1:
                posY = pos[1] + j
                posX = pos[0] + k
                if posY >= 0 and posY < 8:
                    if posX >= 0 and posX < 32:
                        r = rgb[index][j][k][0]
                        g = rgb[index][j][k][1]
                        b = rgb[index][j][k][2]
                        light_pos(strip, posX, posY, r, g, b)
            else:
                posY = pos[1] + j
                posX = pos[0] + k
                if posY >= 0 and posY < 8:
                    if posX >= 0 and posX < 32:
                        r = rgb[index][j][k][0]
                        g = rgb[index][j][k][1]
                        b = rgb[index][j][k][2]
                        light_pos(strip, posX, posY, 0, 0, 0)

def ReadALLA(strip, pos, anima):
    array = anima.GifInfo
    rgb = anima.GifRgb
    FrameNum = len(array)
    height = len(array[0])
    width = len(array[0][0])
    while 1:
        for i in range(FrameNum):
            for j in range(height):
                for k in range(width):
                    posY = pos[1] + j 
                    posX = pos[0] + k
                    if posY >= 0 and posY < 8:
                        if posX >= 0 and posX < 32:
                            r = rgb[i][j][k][0]
                            g = rgb[i][j][k][1]
                            b = rgb[i][j][k][2]
                            light_pos(strip, posX, posY, r, g, b)
            strip.show()
            time.sleep(1/4)

def ReadImg(strip, pos, flag):
    array = Font5X3
    rgb = Rgb5X3
    height = len(array)
    width = len(array[0])
    for j in range(height):
        for k in range(width):
            if array[j][k] == 1:
                posY = pos[1] + j
                posX = pos[0] + k
                if posY >= 0 and posY < 8:
                    if posX >= 0 and posX < 32:
                        r = rgb[j][k][0]
                        g = rgb[j][k][1]
                        b = rgb[j][k][2]
                        if flag == 1:
                            light_pos(strip, posX, posY, r, g, b)
                        else:
                            light_pos(strip, posX, posY, 0, 0, 0)

    strip.show()


def ReadImg2(strip, pos):
    array = Font8X8
    rgb = Rgb8X8
    height = len(array)
    width = len(array[0])
    for j in range(height):
        for k in range(width):
            if array[j][k] == 1:
                posY = pos[1] + j
                posX = pos[0] + k
                if posY >= 0 and posY < 8:
                    if posX >= 0 and posX < 32:
                        r = rgb[j][k][0]
                        g = rgb[j][k][1]
                        b = rgb[j][k][2]
                        light_pos(strip, posX, posY, r, g, b)
    strip.show()


def ReadImg3(strip, pos):
    array = Font1X3
    rgb = Rgb1X3
    height = len(array)
    width = len(array[0])
    for j in range(height):
        for k in range(width):
            if array[j][k] == 1:
                posY = pos[1] + j
                posX = pos[0] + k
                if posY >= 0 and posY < 8:
                    if posX >= 0 and posX < 32:
                        r = rgb[j][k][0]
                        g = rgb[j][k][1]
                        b = rgb[j][k][2]
                        light_pos(strip, posX, posY, r, g, b)

def Clock(strip):
    localtime = time.localtime(time.time())
    hour = localtime.tm_hour
    minute = localtime.tm_min
    second = localtime.tm_sec

   

    x = 12
    y = 1

    ReadImg3(strip, [x+0, 7])
    ReadImg3(strip, [x+4, 7])
    ReadImg3(strip, [x+10, 7])
    ReadImg3(strip, [x+10+4, 7])

    flag = 1
    while 1:
        localtime = time.localtime(time.time())
        hour = localtime.tm_hour
        minute = localtime.tm_min
        second = localtime.tm_sec

    
        ReadALL(strip, int(hour/10), [x+0, y])
        ReadALL(strip, int(hour % 10), [x+4, y])
        ReadImg(strip, [x+7, y], flag)
        ReadALL(strip, int(minute/10), [x+10, y])
        ReadALL(strip, int(minute % 10), [x+10+4, y])
        strip.show()
        time.sleep(0.5)
        flag = 0 - flag



if __name__ == '__main__':

    strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ,
                       LED_DMA, LED_INVERT, LED_BRIGHTNESS)
    strip.begin()
    threads = []
    t1 = threading.Thread(target=Clock,args=(strip,))
    threads.append(t1)
    t2 = threading.Thread(target=ReadALLA,args=(strip, [0, 0], fire,))
    threads.append(t2)

    for t in threads:
        t.setDaemon(True)
        t.start()

    while 1:
        time.sleep(1)